package com.ly.count.login;


import com.ly.count.CPlusConstants;
import com.ly.count.context.CPlusLoginContext;
import com.ly.count.http.CPlusResponse;
import com.ly.count.login.param.CPlusLoginPage;
import com.ly.count.login.param.CPlusLoginParam;
import com.ly.count.login.param.CPlusRegisterParam;
import com.ly.count.login.po.CPlusLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.Cookie;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestBody;
import com.ly.count.service.login.CPlusLoginService;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.ly.count.CPlusConstants.CPLUS_INTERFACE_FOR_WEB;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liaoyong
 * @since 2024-10-03
 */
@RestController
@RequestMapping(CPlusConstants.CPLUS_INTERFACE_FOR_WITHOUILOGIN)
@Tag(name = "游客体验")
public class CPlusLoginController {
    @Autowired
	@Resource(name = "cPlusLoginService")
	private CPlusLoginService cPlusLoginService;
	// 创建日志记录器实例
	private static final Logger logger = LogManager.getLogger(CPlusLoginController.class);

	@Operation(summary = "登录")
    @RequestMapping(method = RequestMethod.POST, value = "/login")
	public CPlusResponse<CPlusLoginContext> detail(@RequestBody CPlusLoginParam cPlusLoginParam){

		ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
		attributes.getRequest().getCookies();
		String cookies = Arrays.stream(Optional.ofNullable(attributes.getRequest().getCookies())
						.orElse(new Cookie[0]))
				.map(cookie -> cookie.getName() + "=" + cookie.getValue())
				.collect(Collectors.joining(","));
		logger.info("Cookies:{}", cookies);
		return cPlusLoginService.login(cPlusLoginParam);
	}

	
	@RequestMapping(method = RequestMethod.POST, value = "/register")
	@Operation(summary = "注册")
	public CPlusResponse<Object> register(@RequestBody CPlusRegisterParam cPlusRegisterParam){
	   
	   return cPlusLoginService.register(cPlusRegisterParam);
	}
/*	@Operation(summary = "编辑")
	@RequestMapping(method = RequestMethod.POST, value = "/modify")
	public CPlusResponse<Object> modify(@RequestBody CPlusLogin cPlusLogin){
	
	   return cPlusLoginService.modify(cPlusLogin);
	}*/
	
}

